package com.situ.shoplook.admin.util;

import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * <p>
 * created by 千堆雪 on 2024/8/10, last modified by 千堆雪 on 2024/8/10
 */
public class CaptchaUtils {
    //设置响应头，清缓存
    public static void setHeader(HttpServletResponse response) {
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
    }

    //输出验证码到字节输出流
    public static void out(Captcha captcha, HttpServletRequest request, HttpServletResponse response) throws IOException {
        setHeader(response);
        request.getSession().setAttribute("captcha", captcha.text().toLowerCase());
        captcha.out(response.getOutputStream());
    }
}
